home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload Trio 2 / Shareware Overload Trio Volume 2 (Chestnut CD-ROM).ISO / dir42 / gnudbm14.zip / GDBMDEFS.H < prev    next >
C/C++ Source or Header  |  1990-08-24  |  11KB  |  295 lines

  1. /* gdbmdefs.h - The include file for dbm.  Defines structure and constants. */
  2.  
  3. /*  This file is part of GDBM, the GNU data base manager, by Philip A. Nelson.
  4.     Copyright (C) 1990  Free Software Foundation, Inc.
  5.  
  6.     GDBM is free software; you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License as published by
  8.     the Free Software Foundation; either version 1, or (at your option)
  9.     any later version.
  10.  
  11.     GDBM is distributed in the hope that it will be useful,
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.     GNU General Public License for more details.
  15.  
  16.     You should have received a copy of the GNU General Public License
  17.     along with GDBM; see the file COPYING.  If not, write to
  18.     the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20.     You may contact the author by:
  21.        e-mail:  phil@wwu.edu
  22.       us-mail:  Philip A. Nelson
  23.                 Computer Science Department
  24.                 Western Washington University
  25.                 Bellingham, WA 98226
  26.         phone:  (206) 676-3035
  27.        
  28. *************************************************************************/
  29.  
  30. /*
  31.  * MS-DOS port (c) 1990 by Thorsten Ohl, td12@@ddagsi3.bitnet
  32.  *
  33.  * To this port, the same copying conditions apply as to the
  34.  * original release.
  35.  *
  36.  * IMPORTANT:
  37.  * This file is not identical to the original GNU release!
  38.  * You should have received this code as patch to the official
  39.  * GNU release.
  40.  *
  41.  * MORE IMPORTANT:
  42.  * This port comes with ABSOLUTELY NO WARRANTY.
  43.  *
  44.  * $Header: e:/gnu/gdbm/RCS/gdbmdefs.h'v 1.4.0.1 90/08/16 09:22:58 tho Exp $
  45.  */
  46.  
  47. #ifdef __STDC__
  48. #include <stdio.h>
  49. #include <stdlib.h>
  50. #include <string.h>
  51. #define VOID void
  52. #else /* not __STDC__ */
  53. #define VOID
  54. #endif /*not __STDC__ */
  55.  
  56. #ifdef MSDOS
  57. #define LONG long
  58. #else /* not MSDOS */
  59. #define LONG int
  60. #endif /* not MSDOS */
  61.  
  62. /* Start with the constant definitions.  */
  63. #define  TRUE    1
  64. #define  FALSE   0
  65.  
  66. /* Parameters to gdbm_open. */
  67. #define  GDBM_READER  0        /* READERS only. */
  68. #define  GDBM_WRITER  1        /* READERS and WRITERS.  Can not create. */
  69. #define  GDBM_WRCREAT 2        /* If not found, create the db. */
  70. #define  GDBM_NEWDB   3        /* ALWAYS create a new db.  (WRITER) */
  71.  
  72. /* Parameters to gdbm_store for simple insertion or replacement in the
  73.    case a key to store is already in the database. */
  74. #define  GDBM_INSERT  0        /* Do not overwrite data in the database. */
  75. #define  GDBM_REPLACE 1        /* Replace the old value with the new value. */
  76.  
  77.  
  78. /* The type definitions are next.  */
  79.  
  80. /* The data and key structure.  This structure is defined for compatibility.  */
  81. typedef struct {
  82.     char *dptr;
  83.     int   dsize;
  84.       } datum;
  85.  
  86.  
  87.  
  88. /* The available file space is stored in an "avail" table.  The one with
  89.    most activity is contained in the file header. (See below.)  When that
  90.    one filles up, it is split in half and half is pushed on an "avail
  91.    stack."  When the active avail table is empty and the "avail stack" is
  92.    not empty, the top of the stack is popped into the active avail table. */
  93.  
  94. /* The following structure is the element of the avaliable table.  */
  95. typedef struct {
  96.       int  av_size;        /* The size of the available block. */
  97.     LONG av_adr;        /* The file address of the available block. */
  98.       } avail_elem;
  99.  
  100. /* This is the actual table. The in-memory images of the avail blocks are
  101.    allocated by malloc using a calculated size.  */
  102. typedef struct {
  103.     int  size;        /* The number of avail elements in the table.*/
  104.     int  count;        /* The number of entries in the table. */
  105.     LONG next_block;    /* The file address of the next avail block. */
  106.     avail_elem av_table[1]; /* The table.  Make it look like an array.  */
  107.       } avail_block;
  108.  
  109. /* In freeing blocks, we will ignore any blocks smaller (and equal) to
  110.    IGNORE_SIZE number of bytes. */
  111. #define IGNORE_SIZE 4
  112.  
  113.  
  114. /* The dbm file header keeps track of the current location of the hash
  115.    directory and the free space in the file.  */
  116.  
  117. typedef struct {
  118.     LONG  header_magic;  /* 0x13579ace to make sure the header is good. */
  119.     int   block_size;    /* The  optimal i/o blocksize from stat. */
  120.     LONG  dir;         /* File address of hash directory table. */
  121.     int   dir_size;         /* Size in bytes of the table.  */
  122.     int   dir_bits;         /* The number of address bits used in the table.*/
  123.     int   bucket_size;   /* Size in bytes of a hash bucket struct. */
  124.     int   bucket_elems;  /* Number of elements in a hash bucket. */
  125.     LONG  next_block;    /* The next unallocated block address. */
  126.     avail_block avail;   /* This must be last because of the psuedo
  127.                 array in avail.  This avail grows to fill
  128.                 the entire block. */
  129.       }  gdbm_file_header;
  130.  
  131.  
  132. /* The dbm hash bucket element contains the full 31 bit hash value, the
  133.    "pointer" to the key and data (stored together) with their sizes.  It also
  134.    has a small part of the actual key value.  It is used to verify the first
  135.    part of the key has the correct value without having to read the actual
  136.    key. */
  137.  
  138. #define SMALL    4
  139.  
  140. typedef struct {
  141.     LONG  hash_value;    /* The complete 31 bit value. */
  142.     char  key_start[SMALL];    /* Up to the first SMALL bytes of the key.  */
  143.     LONG  data_pointer;    /* The file address of the key record. The
  144.                    data record directly follows the key.  */
  145.     int   key_size;        /* Size of key data in the file. */
  146.     int   data_size;    /* Size of associated data in the file. */
  147.       } bucket_element;
  148.  
  149.  
  150. /* A bucket is a small hash table.  This one consists of a number of
  151.    bucket elements plus some bookkeeping fields.  The number of elements
  152.    depends on the optimum blocksize for the storage device and on a
  153.    parameter given at file creation time.  This bucket takes one block.
  154.    When one of these tables gets full, it is split into two hash buckets.
  155.    The contents are split between them by the use of the first few bits
  156.    of the 31 bit hash function.  The location in a bucket is the hash
  157.    value modulo the size of the bucket.  The in-memory images of the
  158.    buckets are allocated by malloc using a calculated size depending of
  159.    the file system buffer size.  To speed up write, each bucket will have
  160.    BUCKET_AVAIL avail elements with the bucket. */
  161.  
  162. #define BUCKET_AVAIL 6
  163.  
  164. typedef struct {
  165.         int   av_count;           /* The number of bucket_avail entries. */
  166.         avail_elem bucket_avail[BUCKET_AVAIL];  /* Distributed avail. */
  167.     int   bucket_bits;       /* The number of bits used to get here. */
  168.     int   count;           /* The number of element buckets full. */
  169.     bucket_element h_table[1]; /* The table.  Make it look like an array.*/
  170.       } hash_bucket;
  171.  
  172. /* We want to keep from reading buckets as much as possible.  The following is
  173.    to implement a bucket cache.  When full, buckets will be dropped in a
  174.    least recently read from disk order.  */
  175.  
  176. /* To speed up fetching and "sequential" access, we need to implement a
  177.    data cache for key/data pairs read from the file.  To find a key, we
  178.    must exactly match the key from the file.  To reduce overhead, the
  179.    data will be read at the same time.  Both key and data will be stored
  180.    in a data cache.  Each bucket cached will have a one element data
  181.    cache.  */
  182.  
  183. typedef struct {
  184.         LONG  hash_val;
  185.     int   data_size;
  186.     int   key_size;
  187.     char *dptr;
  188.     int   elem_loc;
  189.       }  data_cache_elem;
  190.  
  191. #define CACHE_SIZE  100
  192.  
  193. typedef struct {
  194.         hash_bucket *   ca_bucket;
  195.     LONG            ca_adr;
  196.     char        ca_changed;   /* Data in the bucket changed. */
  197.     data_cache_elem ca_data;
  198.       } cache_elem;
  199.  
  200.  
  201.  
  202.  
  203. /* This final structure contains all main memory based information for
  204.    a gdbm file.  This allows multiple gdbm files to be opened at the same
  205.    time by one program. */
  206.  
  207. typedef struct {
  208.     /* Global variables and pointers to dynamic variables used by gdbm.  */
  209.  
  210.       /* The file name. */
  211.     char *name;
  212.  
  213.     /* The reader/writer status. */
  214.     int read_write;
  215.  
  216.     /* The fatal error handling routine. */
  217.     void (*fatal_err) ();
  218.  
  219.     /* The gdbm file descriptor which is set in gdbm_open.  */
  220.     int  desc;
  221.  
  222.     /* The file header holds information about the database. */
  223.     gdbm_file_header *header;
  224.  
  225.     /* The hash table directory from extendible hashing.  See Fagin et al, 
  226.        ACM Trans on Database Systems, Vol 4, No 3. Sept 1979, 315-344 */
  227.     LONG  *dir;
  228.  
  229.     /* The bucket cache. */
  230.     cache_elem bucket_cache [CACHE_SIZE];
  231.     int last_read;
  232.  
  233.     /* Points to the current hash bucket in the cache. */
  234.     hash_bucket *bucket;
  235.  
  236.     /* The directory entry used to get the current hash bucket. */
  237.     int bucket_dir;
  238.  
  239.     /* Pointer to the current bucket's cache entry. */
  240.     cache_elem *cache_entry;
  241.  
  242.  
  243.     /* Bookkeeping of things that need to be written back at the
  244.        end of an update. */
  245.     char  header_changed;
  246.     char  directory_changed;
  247.     char  bucket_changed;
  248.     char  second_changed;
  249.     
  250.       } gdbm_file_info;
  251.  
  252.  
  253. #ifdef __STDC__
  254. extern void gdbm_close (gdbm_file_info *dbf);
  255. extern int gdbm_delete (gdbm_file_info *dbf, datum key);
  256. extern datum gdbm_fetch (gdbm_file_info *dbf, datum key);
  257. extern gdbm_file_info *gdbm_open (char *file, int block_size, int read_write, int mode, void (*fatal_func)());
  258. extern int gdbm_reorganize (gdbm_file_info *dbf);
  259. extern datum gdbm_firstkey (gdbm_file_info *dbf);
  260. extern datum gdbm_nextkey (gdbm_file_info *dbf, datum key);
  261. extern int gdbm_store (gdbm_file_info *dbf, datum key, datum content, int flags);
  262.  
  263. extern VOID _gdbm_new_bucket (gdbm_file_info *dbf, hash_bucket *bucket, int bits);
  264. extern VOID _gdbm_get_bucket (gdbm_file_info *dbf, int dir_index);
  265. extern VOID _gdbm_split_bucket (gdbm_file_info *dbf, LONG next_insert);
  266. extern VOID _gdbm_write_bucket (gdbm_file_info *dbf, cache_elem *ca_entry);
  267. extern LONG _gdbm_alloc (gdbm_file_info *dbf, int num_bytes);
  268. extern int _gdbm_free (gdbm_file_info *dbf, LONG file_adr, int num_bytes);
  269. extern int _gdbm_put_av_elem (avail_elem new_el, avail_elem *av_table, int *av_count);
  270. extern char *_gdbm_read_entry (gdbm_file_info *dbf, int elem_loc);
  271. extern int _gdbm_findkey (gdbm_file_info *dbf, datum key, char **dptr, LONG *new_hash_val);
  272. extern LONG _gdbm_hash (datum key);
  273. extern VOID _gdbm_end_update (gdbm_file_info *dbf);
  274. extern VOID _gdbm_fatal (gdbm_file_info *dbf, char *val);
  275.  
  276. extern  int dbminit (char *file);
  277. extern datum fetch(datum key);
  278. extern int store (datum key, datum content);
  279. extern int delete (datum key);
  280. extern datum firstkey (void);
  281. extern datum nextkey (datum key);
  282.  
  283. extern gdbm_file_info *dbm_open (char *file, int flags, int mode);
  284. extern void dbm_close (gdbm_file_info *dbf);
  285. extern datum dbm_fetch (gdbm_file_info *dbf, datum key);
  286. extern int dbm_store (gdbm_file_info *dbf, datum key, datum content, int flags);
  287. extern int dbm_delete (gdbm_file_info *dbf, datum key);
  288. extern datum dbm_firstkey (gdbm_file_info *dbf);
  289. extern datum dbm_nextkey (gdbm_file_info *dbf);
  290. extern int dbm_dirfno (gdbm_file_info *dbf);
  291. extern int dbm_pagfno (gdbm_file_info *dbf);
  292. #endif /* __STDC__ */
  293.  
  294.  
  295.